#****h* tkjmdict/Makefile
# NAME
#  Makefile
# AUTHOR
#  http://www.iki.fi/petterik/
# SOURCE
#

VERS            = 0.95
MODULE          = tkjmdict
PROJECT         = $(MODULE)-$(VERS)
PROJECT_RELEASE = 1
TARBALL         = $(PROJECT).tar.gz
SRCZIP          = $(PROJECT)-src.zip
ALLZIP          = $(PROJECT).zip
AUTODOC         = robodoc
UPLOAD_FILES    = $(TARBALL) $(SRCTARBALL) $(RPM) $(SRPM)
TCLSH           = tclsh
JMDICT          = JMdict
JMDICT_URL      = http://ftp.monash.edu.au/pub/nihongo/JMdict.gz
MKDIR           = mkdir
RM              = rm -fr
INSTALL         = cp -p
SRCFILES        = tkjmdict Makefile tkjmdict.spec licence.html tkjmdict.html \
                  mk.bat
DICTS           = jmdict_eng.bin jmdict_ger.bin jmdict_fre.bin jmdict_rus.bin

all:
	@echo "try: 'make parse' or 'make package'"

srcpkg: $(SRCTARBALL)
package: $(TARBALL) 
dicts: $(DICTS)
rpm: make_rhdir $(RPM) $(SRPM)

tkjmdict_src.html: tkjmdict
	$(AUTODOC) tkjmdict tkjmdict_src.html HTML

$(DICTS): JMdict
	$(TCLSH) tkjmdict -parse $(JMDICT)

$(SRCTARBALL): $(SRCFILES)
	$(MKDIR) $(PROJECT)
	$(INSTALL) $(SRCFILES) $(PROJECT)
	tar cf - $(PROJECT) | gzip -c > $(SRCTARBALL)
	$(RM) $(PROJECT)

$(TARBALL): $(SRCFILES) $(DICTS)
	$(MKDIR) $(PROJECT)
	$(INSTALL) $(SRCFILES) $(DICTS) $(PROJECT)
	tar cf - $(PROJECT) | gzip -c > $(TARBALL)
	$(RM) $(PROJECT)

tkjmdict.exe: $(DICTS) tkjmdict
	$(INSTALL) tkjmdict tkjmdict.tcl
	freewrap tkjmdict.tcl jmdict_eng.bin jmdict_fre.bin jmdict_ger.bin jmdict_rus.bin jmdict.log -w $(HOME)/bin/freewrap.exe -o tkjmdict.exe
	$(RM) tkjmdict.tcl

.PHONY: parse
parse: $(JMDICT)
	$(TCLSH) tkjmdict -parse $(JMDICT)
	@if [ -f $(HOME)/bin/freewrap.exe ] ; then $(MAKE) tkjmdict.exe ; fi

# get JMdict from Monash server
$(JMDICT):
	curl -s $(JMDICT_URL) | gzip -dc > $(JMDICT)

# correct log date format for tkjmdict.spec file
.PHONY: logdate
logdate:
	date +'* %a %b %d %Y'

.PHONY: clean
clean:
	$(RM) jmdict.log *.bin *.exe *.zip tkjmdict_src.html *.tar.gz *~ JMdict JMdict.gz *.rpm *.deb

.PHONY: make_rhdir
make_rhdir:
	@for i in $(RHDIR) $(RHDIR)/RPMS $(RHDIR)/SOURCES $(RHDIR)/SPECS $(RHDIR)/SRPMS $(RHDIR)/BUILD; do \
	    if [ ! -d $$i ] ; then \
		$(MKDIR) $$i; \
	    fi; \
	    done;

# FIXME
$(RPM) $(SRPM): $(TARBALL)
	cp tkjmdict.spec $(RHDIR)/SPECS
	cp $(TARBALL) $(RHDIR)/SOURCES
	rpmbuild -ba tkjmdict.spec
	cp $(RHDIR)/RPMS/noarch/$(RPM) ./
	cp $(RHDIR)/SRPMS/$(SRPM) ./

checksystem:
	rpm -qa | grep tkjmdict

checkrpm: 
	rpm -qpi $(RPM)
	rpm -qpl $(RPM)

installrpm: rpm
	rpm -i $(RPM)
	rpm -qa | grep tkjmdict

uninstallrpm:
	rpm -e tkjmdict
	rpm -qa | grep tkjmdict

.PHONY: check-freewrap-exe
check-freewrap-exe:
	test -f $(HOME)/bin/freewrap.exe

.PHONY: srczip
srczip: $(SRCFILES)
	rm -f $(SRCZIP)
	zip -q -r $(SRCZIP) $(SRCFILES) licence_files *.jpeg -x 'licence_files/.svn*'

.PHONY: zip
zip: $(SRCFILES) tkjmdict.exe
	$(RM) $(ALLZIP)
	zip -q -r $(ALLZIP) tkjmdict.exe $(SRCFILES) *.jpeg licence_files -x 'licence_files/.svn*'

.PHONY: release
release: check-freewrap-exe tkjmdict.exe srczip zip


#******* end tkjmdict/Makefile
